package wangjg.xuexiban.widget.file;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import wangjg.commons.character.NumberFormat;
import wangjg.xuexiban.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileSelectorAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private File parentFile;
	private List<File> fileList;
	private Bitmap rootIcon;
	private Bitmap parentIcon;
	private Bitmap folderIcon;
	private Bitmap fileIcon;
	
	public FileSelectorAdapter(Context context, List<File> fileList) {
		this.init(context, fileList);
	}

	public FileSelectorAdapter(Context context, File dir) {
		List<File> fileList = new ArrayList<File>();
		if(!"/".equals(dir.getPath())){
			fileList.add(new File("/"));
		}
		if(!"/".equals(dir.getPath()) && !"/".equals(dir.getParent())){
			fileList.add(dir.getParentFile());
			this.parentFile = dir.getParentFile();
		}
		Log.d("canRead", ""+dir.canRead());
		File[] lf = dir.listFiles();
		
		Arrays.sort(lf, new Comparator<File>(){
			public int compare(File lhs, File rhs) {
				if(lhs.isDirectory() != rhs.isDirectory()){
					return lhs.isDirectory()?-1:1;
				}
				return lhs.getName().compareTo(rhs.getName());
			}
		});
		
		if(lf!=null){
			Collections.addAll(fileList, lf);
		}
		this.init(context, fileList);
	}

	private void init(Context context, List<File> fileList) {
		this.mInflater = LayoutInflater.from(context);
		this.fileList = fileList;
		rootIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.fileselector_icon_folder_root);
		parentIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.fileselector_icon_folder_parent);
		folderIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.fileselector_icon_folder);
		fileIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.fileselector_icon_file);
	}

	public int getCount() {
		return fileList.size();
	}

	public Object getItem(int position) {
		return fileList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent){
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.fileselector_row, null);
			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.text = (TextView) convertView.findViewById(R.id.label_file_name);
			holder.size = (TextView) convertView.findViewById(R.id.label_file_size);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		File file = fileList.get(position);
		String lable = file.getName();
		Bitmap icon = null;
		if(lable==null||lable.length()==0){//root
			lable = "根目录";
			icon = rootIcon;
		}else if(file.equals(parentFile)){//
			lable="上级目录..";
			icon = parentIcon;
		}else{
			if(file.isDirectory()){
				icon = folderIcon;
			} else {
				icon = fileIcon;
			}
		}
		holder.icon.setImageBitmap(icon);
		holder.text.setText(lable);
		holder.size.setText("" + NumberFormat.getFileSize(file.length()));
		return convertView;
	}
	
	private class ViewHolder {
		TextView text;
		ImageView icon;
		TextView size;
	}
}
